home *** CD-ROM | disk | FTP | other *** search
/ Computer Inter@ctive 16 / Computer Interactive cdrom 16 - dic 98.iso / zdnetit / content / CMATHCC.ZIP / INCLUDE.ZIP / CFMATH.H < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-01  |  14.3 KB  |  441 lines

  1. /*    CFMATH.H
  2.  
  3.       Complex library for the languages C and C++.
  4.  
  5.       This header file contains all definitions for
  6.       single-precision complex numbers (complex float).
  7.  
  8.       Copyright (C) 1996-1998 Martin Sander
  9.       Address of the author:
  10.            Martin Sander
  11.            Sertⁿrnerstr. 11
  12.            D-37085 G÷ttingen
  13.            Germany
  14. */
  15.  
  16.  
  17. #ifndef __CFMATH_H
  18. #define __CFMATH_H
  19.  
  20. #if !defined( _CMATH_DEFS )
  21.    #ifdef __BORLANDC__
  22.        #pragma option -a-
  23.    #else /* Visual C++ */
  24.        #pragma pack( push,1 )
  25.    #endif /* avoid insertion of dummy bytes  */
  26.    typedef struct {float    Re, Im;} fComplex;
  27.    typedef struct {double   Re, Im;} dComplex;
  28.    #ifdef __BORLANDC__
  29.        typedef long double    extended;
  30.        typedef struct {extended Re, Im;} eComplex;
  31.        #pragma option -a.
  32.    #else /* Visual C++ */
  33.        typedef  double extended; /* Visual C++ does not support
  34.                                  80-bit IEEE numbers. So make
  35.                                  extended equal to double    */
  36.        typedef dComplex  eComplex;
  37.        #pragma pack( pop )
  38.    #endif    /* restore default data packing  */
  39.    typedef fComplex fcomplex;
  40.    typedef dComplex dcomplex;
  41.    typedef eComplex ecomplex;  // tolerate all-lower case
  42.    #define _CMATH_DEFS
  43. #endif
  44. #ifdef __BORLANDC__
  45.     #include <_defs.h>
  46.     #if defined __SMALL__ || defined __MEDIUM__
  47.          #define   _VFAR  near   /* even in case of DS!=SS  */
  48.     #elif defined __FLAT__
  49.          #define  _VFAR
  50.     #else
  51.          #define   _VFAR  far
  52.     #endif
  53.     #if (__BORLANDC__ >= 0x450)
  54.          #define __cmf _RTLENTRY _EXPFUNC
  55.     #else
  56.          #define __cmf  _Cdecl _FARFUNC
  57.     #endif
  58. #else  /* Visual C++, Optima++ */
  59.     #define _VFAR
  60.     #define __cmf  __cdecl
  61. #endif
  62.  
  63.  
  64. /*  first the constructors:  */
  65. #ifdef __cplusplus
  66.    /* since fComplex is declared as a struct instead of a class,
  67.       the constructor cannot get the name "fComplex" here.     */
  68.   #ifndef _FCPLX_DEFINED
  69.   inline fComplex __cmf fcplx( float __ReVal )
  70.   {   fComplex Result;
  71.       Result.Re = __ReVal;
  72.       Result.Im = 0.0f;
  73.       return Result;
  74.   }
  75.  
  76.       // down-conversions from double and extended precision
  77.       // (with OVERFLOW error handling):
  78.   fComplex __cmf fcplx( dComplex __zd );
  79.   #if defined __COMPLEX_H
  80.       fComplex __cmf fcplx( complex __z );
  81.   #endif
  82.   #ifdef __BORLANDC__
  83.       fComplex __cmf fcplx( eComplex __ze );
  84.   #endif
  85.   #define _FCPLX_DEFINED
  86.   #endif  // _FCPLX_DEFINED
  87. extern "C" {
  88. #endif   /* basic form of constructor for C and C++ : */
  89.    fComplex __cmf fcplx( float __ReVal, float __ImVal);
  90.          /* down-conversion with OVERFLOW-handling:  */
  91. #if !defined __NEWCPLX_H
  92.    fComplex __cmf cdtocf( dComplex __zd );
  93.    fComplex __cmf cetocf( eComplex __ze );
  94. #endif
  95.  
  96.    /* Basic complex operations. They are defined both
  97.    for C and C++. However, for C++ you may as well use the
  98.    overloaded operators and functions defined further below. */
  99. #define         cf_real( z )  (z).Re
  100. #define         cf_imag( z )  (z).Im
  101. fComplex __cmf  cf_neg(  fComplex __z );
  102. fComplex __cmf  cf_conj( fComplex __z );
  103. float    __cmf  cf_norm( fComplex __z );
  104. float    __cmf  cf_arg(  fComplex __z );
  105. fComplex __cmf  cf_polar( float __mag, float __angle );
  106.  
  107. fComplex __cmf  cf_add(   fComplex __x, fComplex __y );
  108. fComplex __cmf  cf_addRe( fComplex __x, float __yRe );
  109. fComplex __cmf  cf_sub(   fComplex __x, fComplex __y );
  110. fComplex __cmf  cf_subRe( fComplex __x, float __yRe );  /* x - yRe */
  111. fComplex __cmf  cf_subrRe( fComplex __x, float __yRe ); /* yRe - x */
  112. fComplex __cmf  cf_mul(   fComplex __x, fComplex __y );
  113. fComplex __cmf  cf_mulRe( fComplex __x, float __yRe );
  114. fComplex __cmf  cf_div(   fComplex __x, fComplex __y );
  115. fComplex __cmf  cf_divRe( fComplex __x, float __yRe );   /*  x / yRe  */
  116. fComplex __cmf  cf_divrRe( fComplex __x, float __yRe );  /*  yRe / x  */
  117.  
  118. /*  mathematical functions with error handling through _matherr: */
  119. float    __cmf  cf_abs(  fComplex __z );
  120. fComplex __cmf  cf_acos( fComplex __z );
  121. fComplex __cmf  cf_asin( fComplex __z );
  122. fComplex __cmf  cf_atan( fComplex __z );
  123. fComplex __cmf  cf_cos(  fComplex __z );
  124. fComplex __cmf  cf_cosh( fComplex __z );
  125. fComplex __cmf  cf_cubic( fComplex __z );  /* raise to the third power */
  126. fComplex __cmf  cf_exp(  fComplex __z );
  127. fComplex __cmf  cf_inv(  fComplex __z );    /*   1.0 / z   */
  128. fComplex __cmf  cf_ipow( fComplex __z, int __exponent );
  129.                                              /* raise z to integer power */
  130. fComplex __cmf  cf_ln(    fComplex __z );
  131. fComplex __cmf  cf_log(   fComplex __z ); /* same as cf_ln */
  132. fComplex __cmf  cf_log2(  fComplex __z );
  133. fComplex __cmf  cf_log10( fComplex __z );
  134. fComplex __cmf  cf_pow( fComplex __base, fComplex __exponent );
  135. fComplex __cmf  cf_powReBase( float __base, fComplex __exponent ); /* power of real base */
  136. fComplex __cmf  cf_powReExpo( fComplex __base, float __exponent ); /* raise z to real power */
  137.                          /* for integer exponents, use cf_ipow ! */
  138. fComplex __cmf  cf_quartic( fComplex __z );  /* raise to the fourth power */
  139. fComplex __cmf  cf_sin(  fComplex __z );
  140. fComplex __cmf  cf_sinh( fComplex __z );
  141. fComplex __cmf  cf_square( fComplex __z );
  142. fComplex __cmf  cf_sqrt( fComplex __z );
  143. fComplex __cmf  cf_tan(  fComplex __z );
  144. fComplex __cmf  cf_tanh( fComplex __z );
  145.  
  146.  
  147. #ifdef __cplusplus
  148. }    //  end of the extern "C" statement
  149.  
  150. #if !defined __STD_COMPLEX && !defined __NEWCPLX_H
  151.    /* in addition to the basic operations defined above for C,
  152.       here is the same complete set of overloaded operators and
  153.       functions as offered by the class complex of C++ compilers.     */
  154.  
  155.     inline float real( fComplex & __z )
  156.     {
  157.         return __z.Re;
  158.     }
  159.  
  160.     inline float imag( fComplex & __z )
  161.     {
  162.         return __z.Im;
  163.     }
  164.  
  165.     inline fComplex neg( fComplex & __z1 )
  166.     {   fComplex Result;
  167.         Result.Re = -__z1.Re;
  168.         Result.Im = -__z1.Im;
  169.         return Result;
  170.     }
  171.  
  172.     inline fComplex conj( fComplex & __z)
  173.     {   fComplex Result;
  174.         Result.Re =  __z.Re;
  175.         Result.Im = -__z.Im;
  176.         return Result;
  177.     }
  178.  
  179.     inline float norm( fComplex & __z )
  180.     {
  181.        return __z.Re * __z.Re + __z.Im * __z.Im;
  182.     }
  183.  
  184.     float    __cmf arg(  fComplex __z );
  185.     fComplex __cmf polar( float Mag, float Angle );
  186.  
  187.   //  unary operators:
  188.  
  189.   inline fComplex & operator +( fComplex & __z1 )
  190.   {
  191.       return __z1;
  192.   }
  193.  
  194.   inline fComplex operator -( fComplex & __z1 )
  195.   {   fComplex Result;
  196.       Result.Re = -__z1.Re;
  197.       Result.Im = -__z1.Im;
  198.       return Result;
  199.   }
  200.  
  201.   //  binary operators:
  202.  
  203.   inline fComplex operator +( fComplex & __z1, fComplex & __z2 )
  204.   {   fComplex Result;
  205.       Result.Re = __z1.Re + __z2.Re;
  206.       Result.Im = __z1.Im + __z2.Im;
  207.       return Result;
  208.   }
  209.  
  210.   inline fComplex operator +( fComplex & __z1, float __z2Re )
  211.   {   fComplex Result;
  212.       Result.Re = __z1.Re + __z2Re;
  213.       Result.Im = __z1.Im;
  214.       return Result;
  215.   }
  216.  
  217.   inline fComplex operator +( float __z1Re, fComplex & __z2 )
  218.   {   fComplex Result;
  219.       Result.Re = __z1Re + __z2.Re;
  220.       Result.Im = __z2.Im;
  221.       return Result;
  222.   }
  223.  
  224.   inline fComplex operator -( fComplex & __z1, fComplex & __z2 )
  225.   {   fComplex Result;
  226.       Result.Re = __z1.Re - __z2.Re;
  227.       Result.Im = __z1.Im - __z2.Im;
  228.       return Result;
  229.   }
  230.  
  231.   inline fComplex operator -( fComplex & __z1, float __z2Re )
  232.   {   fComplex Result;
  233.       Result.Re = __z1.Re - __z2Re;
  234.       Result.Im = __z1.Im;
  235.       return Result;
  236.   }
  237.  
  238.   inline fComplex operator -( float __z1Re, fComplex & __z2 )
  239.   {   fComplex Result;
  240.       Result.Re = __z1Re - __z2.Re;
  241.       Result.Im = -__z2.Im;
  242.       return Result;
  243.   }
  244.  
  245.   inline fComplex operator *( fComplex & __z1, fComplex & __z2 )
  246.   {   fComplex Result;
  247.       Result.Re  = __z1.Re * __z2.Re - __z1.Im * __z2.Im;
  248.       Result.Im  = __z1.Re * __z2.Im + __z1.Im * __z2.Re;
  249.       return Result;
  250.   }
  251.  
  252.   inline fComplex operator *( fComplex & __z1, float __z2Re )
  253.   {   fComplex Result;
  254.       Result.Re = __z1.Re * __z2Re;
  255.       Result.Im = __z1.Im * __z2Re;
  256.       return Result;
  257.   }
  258.  
  259.   inline fComplex operator *( float __z1Re, fComplex & __z2 )
  260.   {   fComplex Result;
  261.       Result.Re  = __z1Re * __z2.Re;
  262.       Result.Im  = __z1Re * __z2.Im;
  263.       return Result;
  264.   }
  265.  
  266.   inline fComplex operator /( fComplex & __z1, fComplex & __z2 )
  267.   {   fComplex Result;
  268.       double denom;
  269.       Result.Re = (float)((__z1.Re * __z2.Re + __z1.Im * __z2.Im) /
  270.         (denom = (double)(__z2.Re) * __z2.Re + (double)(__z2.Im) * __z2.Im));
  271.       Result.Im = (float)((__z1.Im * __z2.Re - __z1.Re * __z2.Im ) / denom);
  272.       return Result;
  273.   }
  274.  
  275.   inline fComplex operator /( fComplex & __z1, float __z2Re )
  276.   {   fComplex Result;
  277.       Result.Re = __z1.Re / __z2Re;
  278.       Result.Im = __z1.Im / __z2Re;
  279.       return Result;
  280.   }
  281.  
  282.   inline fComplex operator /( float __z1Re, fComplex & __z2 )
  283.   {   fComplex Result;
  284.       double denom;
  285.       Result.Re = (float)((__z1Re * __z2.Re) /
  286.         (denom = (double)(__z2.Re) * __z2.Re + (double)(__z2.Im) * __z2.Im));
  287.       Result.Im = -(float)((__z1Re * __z2.Im ) / denom);
  288.       return Result;
  289.   }
  290.  
  291.   inline fComplex & operator +=( fComplex & __z1, fComplex & __z2 )
  292.   {
  293.       __z1.Re += __z2.Re;
  294.       __z1.Im += __z2.Im;
  295.       return __z1;
  296.   }
  297.  
  298.   inline fComplex & operator +=( fComplex & __z1, float __z2Re )
  299.   {
  300.       __z1.Re += __z2Re;
  301.       return __z1;
  302.   }
  303.  
  304.   inline fComplex & operator -=( fComplex & __z1, fComplex & __z2 )
  305.   {
  306.       __z1.Re -= __z2.Re;
  307.       __z1.Im -= __z2.Im;
  308.       return __z1;
  309.   }
  310.  
  311.   inline fComplex & operator -=( fComplex & __z1, float __z2Re )
  312.   {
  313.       __z1.Re -= __z2Re;
  314.       return __z1;
  315.   }
  316.  
  317.   inline fComplex & operator *=( fComplex & __z1, fComplex & __z2 )
  318.   {
  319.       float tmpRe;
  320.       tmpRe   = __z1.Re * __z2.Re - __z1.Im * __z2.Im;
  321.       __z1.Im = __z1.Re * __z2.Im + __z1.Im * __z2.Re;
  322.       __z1.Re = tmpRe;
  323.       return __z1;
  324.   }
  325.  
  326.   inline fComplex & operator *=( fComplex & __z1, float __z2Re )
  327.   {
  328.       __z1.Re *= __z2Re;
  329.       __z1.Im *= __z2Re;
  330.       return __z1;
  331.   }
  332.  
  333.   inline fComplex & operator /=( fComplex & __z1, fComplex & __z2 )
  334.   {   double denom;
  335.       float  tmpRe;
  336.       tmpRe = (float)((__z1.Re * __z2.Re + __z1.Im * __z2.Im) /
  337.         (denom = (double)(__z2.Re) * __z2.Re + (double)(__z2.Im) * __z2.Im));
  338.       __z1.Im = (float)((__z1.Im * __z2.Re - __z1.Re * __z2.Im ) / denom);
  339.       __z1.Re = tmpRe;
  340.       return __z1;
  341.   }
  342.  
  343.   inline fComplex & operator /=( fComplex & __z1, float __z2Re )
  344.   {
  345.       __z1.Re /= __z2Re;
  346.       __z1.Im /= __z2Re;
  347.       return __z1;
  348.   }
  349.  
  350.   inline int operator ==( fComplex & __z1, fComplex & __z2 )
  351.   {
  352.       return (__z1.Re == __z2.Re) && (__z1.Im == __z2.Im );
  353.   }
  354.  
  355.   inline int operator ==( fComplex & __z1, float __z2Re )
  356.   {
  357.       return (__z1.Re == __z2Re) && (__z1.Im == 0.0 );
  358.   }
  359.  
  360.   inline int operator !=( fComplex & __z1, fComplex & __z2 )
  361.   {
  362.       return (__z1.Re != __z2.Re) || (__z1.Im != __z2.Im );
  363.   }
  364.  
  365.   inline int operator !=( fComplex & __z1, float __z2Re )
  366.   {
  367.       return (__z1.Im != 0.0 ) || (__z1.Re != __z2Re);
  368.   }
  369.  
  370.  
  371.   /*  C++ version of the mathematical functions defined above.
  372.       They use the same code as the C versions. In case of an error,
  373.       you get a message in which the name of the C version is
  374.       stated.
  375.       Note that these functions require complex arguments to be
  376.       passed by value, not by reference, as it is done in the member
  377.       functions of the class complex. In terms of efficiency, this
  378.       is about the same. (The math functions of the class complex
  379.       store complex results at intermediate addresses and copy them
  380.       to the desired address afterwards. This final copy is not
  381.       necessary here.)                                             */
  382.  
  383.   float    __cmf  abs(  fComplex __z );
  384.   fComplex __cmf  acos( fComplex __z );
  385.   fComplex __cmf  asin( fComplex __z );
  386.   fComplex __cmf  atan( fComplex __z );
  387.   fComplex __cmf  cos(  fComplex __z );
  388.   fComplex __cmf  cosh( fComplex __z );
  389.   fComplex __cmf  cubic( fComplex __z );  /* raise to the third power */
  390.   fComplex __cmf  exp(  fComplex __z );
  391.   fComplex __cmf  inv(  fComplex __z );    /*   1.0 / z   */
  392.   fComplex __cmf  ipow( fComplex __z, int __exponent );
  393.                                             /* raise z to integer power */
  394.   fComplex __cmf  ln(  fComplex __z );
  395.   fComplex __cmf  log(  fComplex __z );  /* same as ln */
  396.   fComplex __cmf  log2( fComplex __z );
  397.   fComplex __cmf  log10( fComplex __z );
  398.   fComplex __cmf  pow( fComplex __z, fComplex __exponent );
  399.   fComplex __cmf  pow( fComplex __z,  float __exponent ); // identical to powReExpo
  400.   fComplex __cmf  pow( float __base,  fComplex __exponent ); // identical to powReBase
  401.   fComplex __cmf  powReBase( float __base, fComplex __exponent ); // power of real base
  402.   fComplex __cmf  powReExpo( fComplex __z, float __exponent );    // raise z to real power
  403.                             // for integer exponents, use ipow !
  404.   fComplex __cmf  quartic( fComplex __z );  // raise to the fourth power
  405.   fComplex __cmf  sin(  fComplex __z );
  406.   fComplex __cmf  sinh( fComplex __z );
  407.   fComplex __cmf  square( fComplex __z );
  408.   fComplex __cmf  sqrt( fComplex __z );
  409.   fComplex __cmf  tan(  fComplex __z );
  410.   fComplex __cmf  tanh( fComplex __z );
  411. #endif   //  __STD_COMPLEX || NEWCPLX_H
  412.  
  413. extern "C" {
  414. #endif  /* __cplusplus  */
  415.  
  416. /***  user-accessible error handling functions, borrowed from VectorLib  ****/
  417.  
  418. void  __cmf  V_noteError( char _VFAR *fname, unsigned why );
  419. void  __cmf  V_printErrorMsg( char _VFAR *ErrMsg );
  420. void  __cmf  V_setErrorEventFile( char _VFAR *filename,  unsigned ScreenAndFile );
  421. void  __cmf  V_closeErrorEventFile( void );
  422.  
  423. /*** translation of calls to matherr() into _matherr() for BorlandC 4.0+ ***/
  424.  
  425. #if defined __BORLANDC__ && (__BORLANDC__ >= 0x450) && !defined (__FLAT__)
  426.      #if !defined( __MATH_H )
  427.             #include <math.h>
  428.      #endif
  429.      int  _Cdecl _FARFUNC matherr (struct exception _VFAR *__e);
  430.      #define NEWMATHERR  \
  431.          int matherr( struct exception _VFAR *__e ) \
  432.          {  return( _matherr( __e )); }
  433. #else
  434.      #define NEWMATHERR
  435. #endif
  436.  
  437. #ifdef __cplusplus
  438. }   // end of extern "C"
  439. #endif
  440. #endif /*  __CFMATH_H  */
  441.